package com.yoosora.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.mikuac.shiro.annotation.MessageHandler;
import com.mikuac.shiro.common.utils.MsgUtils;
import com.mikuac.shiro.common.utils.RegexUtils;
import com.mikuac.shiro.core.Bot;
import com.mikuac.shiro.core.BotPlugin;
import com.mikuac.shiro.dto.event.message.WholeMessageEvent;
import com.yoosora.model.NeteaseSong;
import com.yoosora.utils.ApiConst;
import com.yoosora.utils.Collections;
import com.yoosora.utils.RegexConst;
import com.yoosora.utils.RequestUtils;
import com.yoosora.utils.Strings;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author yoosora
 */
@Component
public class NeteasePlugin extends BotPlugin{

	String test = null;
	
	@MessageHandler(cmd = RegexConst.NETEASE_MUSIC)
	public void neteaseMsg(@NotNull Bot bot, @NotNull WholeMessageEvent event){
		String msg = event.getMessage();
		String sendMsg = Strings.EMPTY;
		String songName = RegexUtils.regexGroup(RegexConst.NETEASE_MUSIC, msg,1);
		if(Strings.isNotEmpty(songName)){
			Map<String,String> param = Maps.newHashMap();
			param.put("s", songName);
			param.put("limit", "1");
			param.put("type", "1");
			param.put("offset", "0");
			String result = RequestUtils.builder().post().build(ApiConst.NETEASE_MUSIC, false, null, param);
			JSONObject jsonObject = JSONObject.parseObject(result);
			NeteaseSong songResult = JSON.parseObject(jsonObject.get("result").toString(), NeteaseSong.class);
			List<Map<String, String>> songs = songResult.getSongs();
			if(null!=songs&&Collections.isNotEmpty(songs)){
				String id = songs.get(0).get("id");
				sendMsg = MsgUtils.builder().music("163", id).build();
			}else{
				sendMsg = "未找到歌曲";
			}
		}else{
			sendMsg = "请使用点歌 歌名进行点歌";
		}
		bot.sendMsg(event, sendMsg, false);
	}
	
	/*@Override
	public int onGroupMessage(@NotNull Bot bot, @NotNull GroupMessageEvent event) {
		String msg = event.getMessage();
		if(msg.matches("测试")){
			String sendMsg = Strings.EMPTY;
			long groupId = event.getGroupId();
			String songName = RegexUtils.regexGroup(RegexConst.NETEASE_MUSIC, msg,1);
			if(Strings.isNotEmpty(songName)){
				Map<String,String> param = Maps.newHashMap();
				param.put("s", songName);
				param.put("limit", "1");
				param.put("type", "1");
				param.put("offset", "0");
				String result = RequestUtils.builder().post().build(ApiConst.NETEASE_MUSIC, false, null, param);
				JSONObject jsonObject = JSONObject.parseObject(result);
				NeteaseSong songResult = JSON.parseObject(jsonObject.get("result").toString(), NeteaseSong.class);
				List<Map<String, String>> songs = songResult.getSongs();
				if(null!=songs&&Collections.isNotEmpty(songs)){
					String id = songs.get(0).get("id");
					sendMsg = MsgUtils.builder().music("163", id).build();
				}else{
					sendMsg = "未找到歌曲";
				}
			}else{
				sendMsg = "请使用点歌 歌名进行点歌";
			}
			bot.sendGroupMsg(groupId,sendMsg,false);
		}
		return MESSAGE_IGNORE;
	}*/
	
	/*public int onGroupMessage(@NotNull Bot bot, @NotNull GroupMessageEvent event) {
		String msg = event.getMessage();
		if(msg.matches(RegexConst.NETEASE_MUSIC)){
			long groupId = event.getGroupId();
			String songName = RegexUtils.regexGroup(RegexConst.NETEASE_MUSIC, msg,1);
			Map<String,String> param = Maps.newHashMap();
			param.put("s", songName);
			param.put("limit", "10");
			param.put("type", "1");
			param.put("offset", "0");
			String result = RequestUtils.builder().post().build(ApiConst.NETEASE_MUSIC, false, null, param);
			JSONObject jsonObject = JSONObject.parseObject(result);
			NeteaseSong songResult = JSON.parseObject(jsonObject.get("result").toString(), NeteaseSong.class);
			List<Map<String, String>> songs = songResult.getSongs();
			List<String> songList = songs.stream().map(o -> MsgUtils.builder().music("163", o.get("id")).build()).collect(Collectors.toList());
			List<Object> list = YoosoraUtils.generateForwardMsg(2315160273L, "博麗の巫女", songList);
			System.out.println(JSON.toJSONString(list));
			bot.sendGroupForwardMsg(groupId,list);
		}
		return MESSAGE_IGNORE;
	}*/
}
